package com.acane.instagram.feature.register

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.acane.instagram.core.model.UserInfo
import com.acane.instagram.core.model.network.datasource.MyRetrofitDatasource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class RegisterViewModel : ViewModel(

) {
    private val _phoneNumber = MutableStateFlow<String> ("")
    val phoneNumber = _phoneNumber
    // 修改手机号
    fun updatePhoneNumber(newPhoneNumber: String) {
        _phoneNumber.value = newPhoneNumber
    }

    private val _email = MutableStateFlow<String> ("")
    val email = _email

    fun updateEmail(newEmail: String) {
        _email.value = newEmail
    }

    private val _userName = MutableStateFlow<String> ("")
    val userName = _userName

    fun updateUserName(newUserName: String) {
        _userName.value = newUserName
    }

    private val _password = MutableStateFlow<String> ("")
    val password = _password

    fun updatePassword(newPassword: String) {
        _password.value = newPassword
    }

    private val _confirmPassword = MutableStateFlow<String> ("")
    val confirmPassword = _confirmPassword

    fun updateConfirmPassword(newConfirmPassword: String) {
        _confirmPassword.value = newConfirmPassword
    }

    fun register(
        userName: String,
        password: String,
        email: String,
        phoneNumber: String
    ) {
        viewModelScope.launch {
            val userInfo = UserInfo(
                userName = userName,
                password = password,
                email = email,
                phone = phoneNumber
            )
            val result = MyRetrofitDatasource.register(userInfo)
            Log.d("RegisterViewModel", "注册成功: $result")
        }
    }
}